package com.example.whatrubbish.controller;

import com.example.whatrubbish.service.SearchRecordService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/searchRecord")
public class SearchRecordController {

    @Autowired
    SearchRecordService searchRecordService;

    @ResponseBody
    @RequestMapping("/select")
    public JsonResult selectAllWithCount(HttpServletRequest request, HttpServletResponse response){
        JsonResult jsonResult;
        try{
            Integer pageIndex = request.getParameter("pageIndex")!=null? request.getParameter("pageIndex")!=""? Integer.parseInt(request.getParameter("pageIndex")) : null : null;
            Integer pageSize = request.getParameter("pageSize")!=null? request.getParameter("pageSize")!=""? Integer.parseInt(request.getParameter("pageSize")) : null : null;
            Criteria criteria = new Criteria();
            PageBean<SelectRecordWithUser> pageBean = new PageBean();
            if(pageIndex != null && pageSize != null){
                pageBean.setPageIndex(pageIndex);
                pageBean.setPageSize(pageSize);
                criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
                criteria.addExtField("pageSize", pageBean.getPageSize());
            }
            criteria.addExtField("userId", request.getParameter("userId")!=null? request.getParameter("userId")!=""? Integer.parseInt(request.getParameter("userId")) : null : null);
            criteria.addExtField("searchName", request.getParameter("searchName")!=null? request.getParameter("searchName")!=""? Integer.parseInt(request.getParameter("searchName")) : null : null);
            List<SelectRecordWithUser> list = searchRecordService.selectSearchRecord(criteria);
            Long total = searchRecordService.selectSearchRecordCount(criteria);
            pageBean.setTotalCount(total);
            pageBean.setResultList(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/hot")
    public JsonResult selectHot(){
        JsonResult jsonResult;
        try{
            List<SelectRecordWithPercent> list = searchRecordService.selectRecordWithPercent();
            for(SelectRecordWithPercent obj : list){
                obj.setPercent(Double.valueOf(String.format("%.1f", obj.getPercent()*100)));
            }
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", list);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/daysRecord")
    public JsonResult daysRecord(){
        JsonResult jsonResult;
        try{
            List<Map<String, Object>> list = searchRecordService.selectFiveDaysRecordCount();
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", list);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error");
        }
        return jsonResult;
    }
}
